package com.jsz.peini.controller.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jsz.peini.bean.codes.SystemCity;
import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.common.util.EhcacheUtil;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.mapper.codes.SystemCityMapper;
import com.jsz.peini.mapper.codes.SystemDistrictMapper;

@Controller
public class CodesController {
	private static final Logger logger = LoggerFactory.getLogger(CodesController.class);
	@Resource
	private SystemDistrictMapper SystemDistrictService;
	@Resource
	private SystemCityMapper systemCityMapper;
	/**
	 * 查询城市列表接口
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getCityList")
	@ResponseBody
	public Object getCityTest(HttpServletRequest request,
			@RequestParam(value="userId",required=false) String userId
			){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		infoMap.put("resultCode", Constants.SUCCESS);
		infoMap.put("resultDesc", "成功");
		try {
			if(StringUtils.isNotBlank(userId)){
				JSONObject obj = PeiniUtils.validToken(userId);
				if(obj.getInteger("resultCode") != 1){
					return obj;
				}
				userId = obj.getString("userId");
				
				HashMap<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("codesOrder", 1);
				//paramMap.put("cityStatus", 1);
				
				List<SystemCity> provinceList = systemCityMapper.findAllByOrderAndStatus(paramMap);
				
				paramMap.put("codesOrder", 2);
				List<SystemCity> cityList = systemCityMapper.findAllByOrderAndStatus(paramMap);
				
				paramMap.put("codesOrder", 3);
				List<SystemCity> countyList = systemCityMapper.findAllByOrderAndStatus(paramMap);
				ArrayList<LinkedHashMap<String,Object>>  areaList = new ArrayList<LinkedHashMap<String,Object>>();
				for(SystemCity province: provinceList){
					LinkedHashMap<String,Object> pmap = new LinkedHashMap<String,Object>();
					pmap.put("provinceId", province.getId());
					pmap.put("provinceName", province.getCodesName());
					ArrayList<LinkedHashMap<String,Object>>  cList = new ArrayList<LinkedHashMap<String,Object>>();
					//循环市
					for(SystemCity city : cityList){
						LinkedHashMap<String,Object> cmap = new LinkedHashMap<String,Object>();
						if(province.getCodesProvince().equals(city.getCodesProvince())){
							cmap.put("cityId", city.getId());
							cmap.put("cityName", city.getCodesName());
							ArrayList<LinkedHashMap<String,Object>>  tList = new ArrayList<LinkedHashMap<String,Object>>();
							//循环区(县)
							for(SystemCity county : countyList){
								LinkedHashMap<String,Object> tmap = new LinkedHashMap<String,Object>();
								if(city.getCodesCity().equals(county.getCodesCity())){
									tmap.put("countyId", county.getCodesCounty());
									tmap.put("countyName", county.getCodesName());
									tList.add(tmap);
								}
								
							}
							cmap.put("cityObject", tList);
							cList.add(cmap);
						}
					}
					pmap.put("provinceObject", cList);
					areaList.add(pmap);
				}
				infoMap.put("areaCity", areaList);
			}else{
				Object city = EhcacheUtil.getInstance().get("codeCache", "areaList");
				infoMap.put("areaCity", city);
			}
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 商圈热门商圈列表接口
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getDistrictList")
	@ResponseBody
	public Object getDistrictList(HttpServletRequest request){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		infoMap.put("resultCode", Constants.SUCCESS);
		infoMap.put("resultDesc", "成功");
		try {
			Object object = EhcacheUtil.getInstance().get("codeCache", "districtList");
			List<LinkedHashMap<String,Object>> districtHotList = SystemDistrictService.getDistrictList();
			infoMap.put("districtHotList", districtHotList);
			infoMap.put("districtList", object);
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}

}
